/**
 * Project Name:fire-industry-DM-center
 * File Name:GlobalExceptionHandler.java
 * Package Name:com.firestone.config.mvc
 * Date:2018年3月12日下午3:09:28
 *
*/

package com.firestone.config.mvc;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MaxUploadSizeExceededException;
import org.springframework.web.servlet.NoHandlerFoundException;

import com.firestone.bean.dto.ReturnDTO;
import com.firestone.core.enums.SystemCodeEnum;
import com.firestone.core.exception.FireAccessDeniedException;
import com.firestone.core.exception.FireException;

/**
 * @Description: TODO(系统统一异常处理)
 * @author CAIYJ
 * @param <T>
 * @date 2018年3月12日 下午3:09:28
 */
@ControllerAdvice
public class GlobalExceptionHandler<T> {

    private static final Logger loggger = LogManager
            .getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public ReturnDTO<?> defaultErrorHandler(HttpServletRequest req, Exception e)
            throws Exception {
        loggger.error(
                SystemCodeEnum.http_status_internal_server_error.getErrorMsg(),
                e);
        if (e instanceof NoHandlerFoundException) {
            return ReturnDTO.fail(SystemCodeEnum.http_status_not_found);
        }
        return ReturnDTO.fail(SystemCodeEnum.http_status_internal_server_error);
    }

    @ExceptionHandler(value = MaxUploadSizeExceededException.class)
    @ResponseBody
    public ReturnDTO<String> maxUploadSizeExceededExceptionHandler(
            HttpServletRequest req, MaxUploadSizeExceededException e)
                    throws Exception {
        long maxSize = ((MaxUploadSizeExceededException) e).getMaxUploadSize();
        loggger.error(SystemCodeEnum.error_file_upload.getErrorMsg(), e);
        return new ReturnDTO<String>(
                SystemCodeEnum.error_file_upload.getErrorCode(),
                SystemCodeEnum.error_file_upload.getErrorMsg(),
                "上传文件太大，不能超过" + maxSize / 1024 + "k",
                SystemCodeEnum.error_file_upload.getErrorMsg());
    }

    @ExceptionHandler(value = FireAccessDeniedException.class)
    @ResponseBody
    public ReturnDTO<String> FireAccessDeniedExceptionHandler(
            HttpServletRequest req, FireAccessDeniedException e)
                    throws Exception {
        loggger.error(SystemCodeEnum.http_status_forbidden.getErrorMsg(), e);
        return new ReturnDTO<String>(
                SystemCodeEnum.http_status_forbidden.getErrorCode(),
                e.getMessage(), e.getMessage(), e.getMessage());
    }

    @ExceptionHandler(value = FireException.class)
    @ResponseBody
    public ReturnDTO<String> FireExceptionHandler(HttpServletRequest req,
            FireException e) throws Exception {
        loggger.error(e.getErrorCode(), e);
        return new ReturnDTO<String>(e.getErrorCode(), e.getMessage(),
                e.getMessage(), e.getMessage());
    }
}
